<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyle.html">
</head>
<body>
<title>分管授权</title>
<div class="bpmAuthorize" style="position: relative;height: 100%;">
    <div style="position: absolute;top:0;right:0px;bottom: 0;left: 0px;padding:10px">
        <table id="bpmDefAuthorize" lay-filter="bpmDefAuthorize"></table>
    </div>
</div>

<script type="text/html" id="headerToolsBar">
    <div class="ccpui-enterSel">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-sm" lay-event="query">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            <span>新增</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
            <span>编辑</span>
        </a>
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            <span>删除</span>
        </a>
        <a class="layui-btn layui-btn-sm" lay-event="reset">
            <i class="layui-icon layui-icon-refresh"></i>
            <span>重置</span>
        </a>
    </div>
    <form class="layui-form" action="" lay-filter="queryForm" style="margin-top:10px">
        <div class="table-top-filter">
            <div class="table-filter-alone">
                <label class="layui-form-label">授权人员名称</label>
                <input type="text" name="Q_user_SL" class="layui-input filter-value" placeholder="授权人员名称"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">授权流程名称</label>
                <input type="text" name="Q_act_SL" class="layui-input filter-value" placeholder="授权流程名称"/>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">权限类型</label>
                <select type="text" name="Q_authType_S" id="status" class="layui-input filter-value">
                    <option value="">所有</option>
                    <option value="start">启动</option>
                    <option value="management">定义</option>
                    <option value="task">任务</option>
                    <option value="instance">实例</option>
                </select>
            </div>
            <div class="table-filter-alone">
                <label class="layui-form-label">权限描述</label>
                <input type="text" name="Q_desc_SL" id="name" class="layui-input filter-value" placeholder="权限描述"/>
            </div>
        </div>
    </form>
    </div>
</script>
<script type="text/html" id="bpmDefAuthorizeBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
</script>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table','commons','layer','setter','common','totalFilter'],function(){
        var table = layui.table;
        var commons = layui.commons;
        var layer = layui.layer;
        var setter = layui.setter;
        var common = layui.common;
        var totalFilter = layui.totalFilter;
        var $ = layui.$;

        var tableH = commons.tabHeight();

        loadTable(serverPath.workflowPath + setter.workflowLinkPath + 'bpmDefAuthorize/list');

        function loadTable(url){
            table.render({
                elem: '#bpmDefAuthorize'
                , title: '分管授权列表'
                , height: tableH
                , url: url
                , method: 'post'
                , toolbar: '#headerToolsBar'
                , page: true
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'authorizeDesc', title: '权限描述', align: 'center', width: 250}
                    , {field: 'authUser', title: '授权人员名称', align: 'center',templet: function(d){
                        var user = d.authUser;
                        var html = '';
                        layui.each(user,function(index,item){
                            if(item.rightType === 'all'){
                                html += '<span class="" style="margin-right: 5px;">所有用户</span>';
                            }else if(item.rightType === 'user'){
                                html += '<a onclick=authUserClick("'+ item.ownerId +'") href="javascript:void(0);" class="" style="color: blue;margin-right: 5px;">'+ item.ownerName +'</a>';
                            }else{
                                html += '<span class="" style="margin-right: 5px;">'+ item.ownerName +'</span>';
                            }
                        });
                        return html === '' ? '暂无授权人员' : html;
                       }
                    }
                    , {field: 'authAct', title: '流程名称', align: 'center', templet: function(d){
                        var act = d.authAct;
                        var html = '';
                        layui.each(act,function(index,item){
                            html += '<a href="javascript:void(0);" onclick=onAuthActClick("'+ item.defKey +'") class="layui-badge" style="margin-right: 5px;">'+ item.defName +'</a>';
                        });
                        return html === '' ? '暂无授权流程' : html;
                       }
                    }
                    , {field: 'authType', title: '授权类型', align: 'center', templet: function(d){
                         var type = d.authType;
                         var html = '';
                         layui.each(type,function(index,item){
                             switch (item.authorizeType) {
                                 case 'start':
                                     html += '<span style="margin-right: 10px;">启动</span>';
                                     break;
                                 case 'management':
                                     html += '<span style="margin-right: 10px;">定义</span>';
                                     break;
                                 case 'task':
                                     html += '<span style="margin-right: 10px;">任务</span>';
                                     break;
                                 case 'instance':
                                     html += '<span style="margin-right: 10px;">实例</span>';
                                     break;
                             }
                         });
                         return html === '' ? '暂无流程授权类型' : html;
                       }
                    }
                    , { title: '管理', toolbar: '#bpmDefAuthorizeBar', width: 160, align: 'center'}
                ]]
                ,done:function () {
                    common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel')
                }
            });
            totalFilter.init('bpmDefAuthorize');
        }

        // 新增或修改弹窗
        function addOrUpdateHandle(type,id){
            layer.open({
                type: 2,
                title: type === 'add' ? '新增' : '修改',
                area: ['790px', '560px'],
                content: serverPath.workflowPath + setter.workflowLinkPath + 'bpmDefAuthorize/view/add' + (id ? '?id='+id : ''),
                skin: 'btn-class',
                btn: ['保存','取消'],
                btnAlign: 'c',
                yes: function(index,layero){
                    //获取iframe的body元素
                    var body = layer.getChildFrame('body',index);
                    var addWin = window[layero.find('iframe')[0]['name']];
                    var authorizeDesc = body.find("#authorizeDesc").val();
                    if(authorizeDesc === '' || authorizeDesc === null){
                        layer.msg('权限描述不能为空', {time: 2500, icon:5});
                        return false;
                    }
                    $.get(serverPath.workflowPath + setter.workflowLinkPath + 'bpmDefAuthorize/getCountByDesc',
                      {desc: authorizeDesc},
                      function(data){

                          if(data.code !== 0){
                              layer.msg(data.msg, {time: 2500, icon:5});
                              return false;
                          }
                          if(!id && data.data > 0){
                              layer.msg('权限描述名称已存在', {time: 2500, icon:5});
                              return false;
                          }else{
                              var ajaxData = {
                                  authorizeDesc: authorizeDesc,
                                  authorizeTypes: addWin.layui.authorizeTypesData.join(','),
                                  authorizeUsers: JSON.stringify(addWin.layui.authorizeUserTableData),
                                  authorizeActs: JSON.stringify(addWin.layui.authorizeActTableData)
                              };
                              if(id){
                                  ajaxData['id'] = id;
                              }
                              var saveLoadingLayer = layer.load(1);
                              $.ajax({
                                  url: serverPath.workflowPath + setter.workflowLinkPath + 'bpmDefAuthorize/add',
                                  method: 'post',
                                  dataType: 'json',
                                  data: ajaxData,
                                  success: function(data){
                                      layer.close(saveLoadingLayer);
                                      if(data.code !== 0){
                                          layer.msg(data.msg, {time: 2500, icon:5});
                                          return false;
                                      }
                                      layer.msg(data.msg, {time: 1000, icon:6}, function(){
                                          layer.close(index);
                                          table.reload('bpmDefAuthorize',{})
                                      });
                                  },
                                  error: function (XMLHttpRequest, textStatus, errorThrown) {
                                      layer.close(saveLoadingLayer);
                                      layer.alert("保存失败！" + XMLHttpRequest.status);
                                  }
                              });
                          }
                    },"json");
                }
            });
        }

        // 监听头工具栏事件
        table.on('toolbar(bpmDefAuthorize)',function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("bpmDefAuthorize","queryForm",{where:{}},false);
                    break;
                case 'add':
                    addOrUpdateHandle('add');
                    break;
                case 'edit':
                    if(data.length !== 1){
                        layer.msg("请选择一项数据!", {time: 2500, icon:5});
                        return false;
                    }
                    addOrUpdateHandle('edit',data[0].id);
                    break;
                case 'del':
                    if(data.length === 0){
                        layer.msg("请选择一项数据!", {time: 2500, icon:5});
                        return false;
                    }
                    var ids = $.map(data,function(v){
                        return v.id;
                    });
                    layer.confirm("确认删除吗?",{icon:0},function(index){
                        layer.close(index);
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + 'bpmDefAuthorize/delBatch',
                            type: 'post',
                            dataType: 'json',
                            data: {
                                ids: ids
                            },
                            success: function(data){
                                if(data.code !== 0){
                                    layer.msg(data.msg, {time: 2500, icon:5});
                                    return false;
                                }
                                layer.msg(data.msg, {time: 500, icon:6});
                                table.reload('bpmDefAuthorize',{page: {curr: 1}});
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert("删除失败！" + XMLHttpRequest.status);
                            }
                        });
                    });
                    break;
                case 'reset':
                    common.tableConditionRender("bpmDefAuthorize","queryForm",{where:{}},true);
                    break;
            }
        });

        // 监听头行工具事件
        table.on('tool(bpmDefAuthorize)',function(obj){
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    addOrUpdateHandle('edit',data.id);
                    break;
                case 'del':
                    layer.confirm("确认删除吗?",{icon:0},function(index){
                        layer.close(index);
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + 'bpmDefAuthorize/del',
                            type: 'post',
                            dataType: 'json',
                            data: {
                                id: data.id
                            },
                            success: function(data){
                                if(data.code !== 0){
                                    layer.msg(data.msg, {time: 2500, icon:5});
                                    return false;
                                }
                                layer.msg(data.msg, {time: 1000, icon:6});
                                table.reload('bpmDefAuthorize',{page: {curr: 1}});
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert("删除失败！" + XMLHttpRequest.status);
                            }
                        });
                    });
                    break;
                case 'detail':
                    layer.open({
                        type: 2,
                        title: '详情',
                        area: ['790px', '560px'],
                        content: serverPath.workflowPath + setter.workflowLinkPath + 'bpmDefAuthorize/view/add?type=detail&id=' + data.id,
                        skin: 'btn-close',
                        btn: ['关闭'],
                        btnAlign: 'c',
                        yes: function (index, layero) {
                            layer.close(index);
                        }
                    });
                    break;
            }
        });

        // 点击授权人员弹窗
        window.authUserClick = function(ownerId){
            parent.layer.open({
                type: 2,
                title: '人员详细信息',
                area: ['938px', '620px'],
                shadeClose: true,
                content: serverPath.systemPath + setter.sysLinkPath + 'sysUser/view/view?userId=' + ownerId
            });
        };
        // 点击流程名称弹窗
        window.onAuthActClick = function(defKey){
            $.ajax({
                url: serverPath.workflowPath + setter.workflowLinkPath + 'actDefModel/getModelIdByDefKey?defKey=' + defKey,
                type: 'get',
                dataType: 'json',
                success: function(data){
                    if(data.code !== 0){
                        layer.msg(data.msg, {time: 2500, icon:5});
                        return false;
                    }
                    var url = serverPath.workflowPath + setter.workflowLinkPath + 'actDefModel/view/detail?modelId=' + data.data.id;
                    //Ajax获取
                    $.post(url, {}, function(str){
                        layer.open({
                            type: 1,
                            title: '流程详细信息',
                            area: ['1100px', '700px'],
                            shadeClose: true,
                            content: str,
                            btn:['关闭'],
                            skin: 'btn-close'
                        });
                    });
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("获取流程信息失败！" + XMLHttpRequest.status);
                }
            });
        }
    });
</script>
<style>
    .layui-table-tool-self {
        display: none;
    }
    .bpmAuthorize .table-top-filter div input {
        border-radius: 4px;
        float: left;
        width: 170px;
    }
    .bpmAuthorize .table-top-filter label.layui-form-label {
        width: 114px !important;
        float: left;
    }
    .btn-close .layui-layer-btn{
        text-align: center;
    }
    .btn-close .layui-layer-btn a{
        display: inline-block;
        height: 38px;
        line-height: 38px;
        padding: 0 18px;
        background-color: #FF5722;
        color: #fff;
        white-space: nowrap;
        text-align: center;
        font-size: 14px;
        border: none;
        border-radius: 2px;
        cursor: pointer;
    }
</style>
</body>
</html>